﻿using AnlianCommon.API.Commands.BasicInfos;
using AnlianCommon.API.Commands.Personnels;
using AnlianCommon.API.Commands.RolePerms;
using AnlianCommon.API.Commands.Roles;
using AnlianCommon.API.Commands.UserLogics;
using AnlianCommon.Common.Domains.UserPermissions;
using AnlianCommon.Common.DTO;
using AnlianCommon.Enum;
using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.IdentityModel.Tokens;
using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
using System.Globalization;
using System.IdentityModel.Tokens.Jwt;
using System.Text;
using System.Web.Http.Results;

namespace AnlianCommon.API.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    //[Authorize]
    public class UsersInterfaceController : ControllerBase
    {
        private readonly IMediator _mediator;

        public UsersInterfaceController(IMediator mediator)
        {
            _mediator = mediator;
        }
        /// <summary>
        /// 登录方法
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        [HttpPost]
        //[AllowAnonymous]
        public async Task<Result> UserLogin(UserLogicsQueryCommand request, CancellationToken cancellationToken)
        {
            try
            {
                var result = await _mediator.Send(request);
                //var token = CreateTokenString();
                return result;
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 个人信息反填
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<Result> UserInfoBackFill(UserLogicsBackFillCommand request, CancellationToken cancellationToken)
        {
            var result = await _mediator.Send(request);
            return result;
        }
        /// <summary>
        /// 导入人员信息
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<Result> PersonnelsImport([FromQuery]PersonnelsImportCommand request, CancellationToken cancellationToken)
        {
            return await _mediator.Send(request);
            
        }
        /// <summary>
        /// 修改个人信息用户名-只能修改一次
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<Result> UpdateUserName(UserLogicsUpdateCommand request, CancellationToken cancellationToken)
        {
            var result = await _mediator.Send(request);   
            return result;
        }
        /// <summary>
        /// 添加角色信息
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<Result> AddRole(RoleCreateCommand request)
        {
             var result = await _mediator.Send(request);
             return result;
        }
        /// <summary>
        ///  角色列表分页查询
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ResultPage<Role>> RoleLists(RoleQueryCommand request, CancellationToken cancellationToken)
        {
            var result =await _mediator.Send(request);
            return result;
        }
        /// <summary>
        /// 角色信息删除
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<Result> UpdateRoles(RoleDeletedCommand request, CancellationToken cancellationToken)
        {
            var result = await _mediator.Send(request);
            return result;
        }
        /// <summary>
        ///角色权限删除
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<Result> RolePermQuery(RolePermDeletedCommand request, CancellationToken cancellationToken)
        {
            var result = await _mediator.Send(request);
            return result;
        }
        /// <summary>
        /// 角色权限列表
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ResultPage<RolePermDto>> RolePermQuerys(RolePermQueryCommand request, CancellationToken cancellationToken)
        {
            var result = await _mediator.Send(request);
            return result;
        }
        /// <summary>
        /// 添加人员信息
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<Result> AddPersonnel(PersonnelCreateCommand request, CancellationToken cancellationToken)
        {
            var result = await _mediator.Send(request);
            return result;
        }
        /// <summary>
        /// 反填人员信息
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<Result> FantianPersonnel(PersonnelBackFillCommand request, CancellationToken cancellationToken)
        {
            var result = await _mediator.Send(request);
            return result;
        }
        /// <summary>
        /// 添加人员基本信息
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<Result> AddBasicInfo(BasicInfoCreateCommand request, CancellationToken cancellationToken)
        {
            var result = await _mediator.Send(request);
            return result;
        }
        /// <summary>
        /// 人员信息列表
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ResultPage<Personnel>> PersonnelSeachs(PersonnelSeachCommand request, CancellationToken cancellationToken)
        {
            var result = await _mediator.Send(request);
            return result;
        }



        /// <summary>
        /// 生成 JWT Token（令牌）
        /// </summary>
        /// <returns></returns>
        private string CreateTokenString()
        {
            //私钥
            var secretByte = Encoding.UTF8.GetBytes("kdsfldsflkdslkflkdsflkdslfdslkflk");
            // 非对称加密
            var signingKey = new SymmetricSecurityKey(secretByte);
            // 使用256 生成数字签名
            var signingCredentials = new SigningCredentials(signingKey, SecurityAlgorithms.HmacSha256);
            // 生成Token
            var token = new JwtSecurityToken(
                issuer: "BeiJingBW",
                audience: "BeiJingBW",
                expires: DateTime.Now.AddDays(1), // 一天后过期
                signingCredentials: signingCredentials
            );
            // 生成token 字符串
            var strToken = new JwtSecurityTokenHandler().WriteToken(token);
            return strToken;
        }


    }
}
